const enWords = require("../sequelize/enWords");

class Service {
    async list(query) {
        try {
            const pageNum = parseInt(query.pageNum) || 1;
            const pageSize = parseInt(query.pageSize) || 10;
            const rows = await enWords.findAndCountAll({});
            return {
                code: 200,
                message: "ok",
                result: {
                    list: await enWords.findAll({
                        limit: pageSize,
                        offset: (pageNum - 1) * pageSize,
                    }),
                    count: rows.count,
                },
            };
        } catch (err) {
            console.error(err);
            throw new Error(err);
        }
    }

    async add(body) {
        try {
            await enWords.create({ ...body });
            return {
                code: 200,
                message: "ok",
                result: {},
            };
        } catch (err) {
            console.error(err);
            throw new Error(err);
        }
    }

    async update(body) {
        try {
            await enWords.update(body, { where: { id: body.id } });
            return {
                code: 200,
                message: "ok",
                result: {},
            };
        } catch (err) {
            console.error(err);
            throw new Error(err);
        }
    }

    async del(id) {
        try {
            await enWords.destroy({
                where: {
                    id,
                },
            });
            return {
                code: 200,
                message: "ok",
                result: {},
            };
        } catch (err) {
            console.error(err);
            throw new Error(err);
        }
    }
}

module.exports = new Service();
